package com.ruoyi.web.controller.config;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.entity.CatalogueTree;
import com.ruoyi.system.service.CatalogueTreeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 文件管理目录树(CatalogueTree)表控制层
 * @since 2023-03-20 15:22:42
 */
@RestController
@RequestMapping("catalogueTree")
public class CatalogueTreeController extends BaseController {

    @Resource
    private CatalogueTreeService catalogueTreeService;

    @GetMapping("/list")
    public TableDataInfo queryByPage(CatalogueTree catalogueTree) {
        startPage();
        List<CatalogueTree> list = catalogueTreeService.queryAll(catalogueTree);
        return getDataTable(list);
    }

    @GetMapping("/tree")
    public ResponseEntity<List<CatalogueTree>> tree() {
        List<CatalogueTree> list = catalogueTreeService.tree();
        return ResponseEntity.ok(list);
    }

    /**
     * 分页查询
     *
     * @param catalogueTree 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<CatalogueTree>> queryByPage(CatalogueTree catalogueTree, PageRequest pageRequest) {
        return ResponseEntity.ok(this.catalogueTreeService.queryByPage(catalogueTree, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<CatalogueTree> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.catalogueTreeService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param catalogueTree 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity<CatalogueTree> add(@Validated @RequestBody CatalogueTree catalogueTree) {
        return ResponseEntity.ok(this.catalogueTreeService.insert(catalogueTree));
    }

    /**
     * 编辑数据
     *
     * @param catalogueTree 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public ResponseEntity<CatalogueTree> edit(@Validated @RequestBody CatalogueTree catalogueTree) {
        return ResponseEntity.ok(this.catalogueTreeService.update(catalogueTree));
    }

    /**
     * 删除数据
     *
     * @param catalogueTree 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody CatalogueTree catalogueTree) {
        return ResponseEntity.ok(this.catalogueTreeService.deleteById(catalogueTree.getId()));
    }

}

